﻿using System.Collections.Generic;
using System.Linq;
using EmployeeApp.DAL.Entities;
using EmployeeApp.DAL.Models.Branch;
using NHibernate;
using NHibernate.Criterion;

namespace EmployeeApp.DAL.Dal
{
   public class BranchDAL
    {
       public ISession Session;
       public BranchDAL(ISession session)
        {
            Session = session; 
        }

       public List<Branch> GetAll(SearchCriteria searchCriteria)
       {
           var criteria = Session.CreateCriteria(typeof(Branch));

           if (searchCriteria != null)
           {
               // Search
               if (!string.IsNullOrEmpty(searchCriteria.Code))
               {
                   criteria.Add(Restrictions.Like("Code", searchCriteria.Code, MatchMode.Start));
               }
               if (!string.IsNullOrEmpty(searchCriteria.Name))
               {
                   criteria.Add(Restrictions.Like("Name", searchCriteria.Name, MatchMode.Start));
               }

               //Sort
               if (!string.IsNullOrEmpty(searchCriteria.Sort))
               {
                   if (!string.IsNullOrEmpty(searchCriteria.SortType) && searchCriteria.SortType.ToLower() == "desc")
                   {
                       criteria.AddOrder(Order.Desc(searchCriteria.Sort));
                   }
                   else
                   {
                       criteria.AddOrder(Order.Asc(searchCriteria.Sort));
                   }

               }
           }

           return criteria.List<Branch>().ToList();
       }
    }
}
